<template>
  <div>
    <h1>DataNode Edit</h1>

    <el-form label-width="150px">
      <el-form-item label="name">
        <el-input v-model="dataNode.name"></el-input>
      </el-form-item>
      <el-form-item label="url">
        <el-input v-model="dataNode.url"></el-input>
      </el-form-item>
      <el-form-item label="username">
        <el-input v-model="dataNode.username"></el-input>
      </el-form-item>
      <el-form-item label="password">
        <el-input v-model="dataNode.password"></el-input>
      </el-form-item>
      <el-form-item label="description">
        <el-input v-model="dataNode.description"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="$router.go(-1)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Message } from 'element-ui';

export default {
  data() {
    return {
      dataNode: {}
    }
  },

  methods: {
    loadById() {
      if (!this.$route.query.id) {
        return;
      }

      var _this = this;
      sdk.DataNodeApi().findById({
        id: _this.$route.query.id
      }).then(res => {
        _this.dataNode = res;
      });
    },

    onSubmit() {
      if (!!this.$route.query.id) {
        this.updateIgnoreNull();
        return;
      }

      this.insertIgnoreNull();
    },

    insertIgnoreNull() {
      var _this = this;
      sdk.DataNodeApi().insertIgnoreNull({
        dataNode: _this.dataNode
      }).then(res => {
        console.log('#65 insertIgnoreNull', res);
        Message('添加成功');
      });
    },

    updateIgnoreNull() {
      var _this = this;
      sdk.DataNodeApi().updateIgnoreNull({
        dataNode: _this.dataNode
      }).then(res => {
        console.log('#77 updateIgnoreNull', res);
        Message('更新成功');
      });
    }
  },

  mounted(){
    this.loadById();
  }
};
</script>

<style>
</style>